﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DrugStorehouseMIS.DAL;
using DrugStorehouseMIS.Comm;
using DrugStorehouseMIS.DAL.Interface;

namespace DrugStorehouseMIS.BLL
{
    public class Drug
    {
        #region 封装属性

        private int mDrugNum;

        public int DrugNum
        {
            get { return mDrugNum; }
            set { mDrugNum = value; }
        }

        private DrugType mDrugTypeNum; //药品类型

        public DrugType DrugTypeNum
        {
            get { return mDrugTypeNum; }
            set { mDrugTypeNum = value; }
        }

        private string mTradeName; //商品名

        public string TradeName
        {
            get { return mTradeName; }
            set { mTradeName = value; }
        }

        private string mCommonName;//常用名

        public string CommonName
        {
            get { return mCommonName; }
            set { mCommonName = value; }
        }

        private string mUserDefindeName;//用户自定义名

        public string UserDefindeName
        {
            get { return mUserDefindeName; }
            set { mUserDefindeName = value; }
        }

        private string mDrugCode; //药品编码

        public string DrugCode
        {
            get { return mDrugCode; }
            set { mDrugCode = value; }
        }

        private string mSepcifications;//规格

        public string Sepcifications
        {
            get { return mSepcifications; }
            set { mSepcifications = value; }
        }

        private MeasuringUnit mMeasuringUnitNum; //单位

        public MeasuringUnit MeasuringUnitNum
        {
            get { return mMeasuringUnitNum; }
            set { mMeasuringUnitNum = value; }
        }

        private string mApprovalNum; //批准文号

        public string ApprovalNum
        {
            get { return mApprovalNum; }
            set { mApprovalNum = value; }
        }

        private int mMinQuantity; //库存下限

        public int MinQuantity
        {
            get { return mMinQuantity; }
            set { mMinQuantity = value; }
        }

        private int mMaxQuantity; //库存上限

        public int MaxQuantity
        {
            get { return mMaxQuantity; }
            set { mMaxQuantity = value; }
        }

        private float mTradePrice;

        public float TradePrice
        {
            get { return mTradePrice; }
            set { mTradePrice = value; }
        }

        private float mRetailPrice;

        public float RetailPrice
        {
            get { return mRetailPrice; }
            set { mRetailPrice = value; }
        }

        #endregion

        private IDrugDAL drugDal;

        public Drug()
        {
            drugDal = Adapter.GetDrug();
        }

        public void FetchObject(IDataReader dr)
        {
            if (dr != null)
            {
                mDrugNum = ConvertDataType.CheckFiledInt(dr["drug_num"]);
                mDrugTypeNum.DrugTypeNum = ConvertDataType.CheckFiledInt(dr["drug_type_num"]);
                mDrugCode = ConvertDataType.CheckFiledString(dr["drug_code"]);
                mApprovalNum = ConvertDataType.CheckFiledString(dr["approval_num"]);
                mCommonName = ConvertDataType.CheckFiledString(dr["common_name"]);
                mMeasuringUnitNum.MeasuringUnitNum = ConvertDataType.CheckFiledInt(dr["measuring_unit_num"]);
                mSepcifications = ConvertDataType.CheckFiledString(dr["specifications"]);
                mTradeName = ConvertDataType.CheckFiledString(dr["trade_name"]);
                mUserDefindeName = ConvertDataType.CheckFiledString(dr["user_defined_name"]);
                mMaxQuantity = ConvertDataType.CheckFiledInt(dr["max_quantity"]);
                mMinQuantity = ConvertDataType.CheckFiledInt(dr["min_quantity"]);
                mTradePrice = ConvertDataType.CheckFieldFloat(dr["trade_price"]);
                mRetailPrice = ConvertDataType.CheckFieldFloat(dr["retail_price"]);
            }
        }

        /// <summary>
        /// 根据药品id获取一种药品
        /// </summary>
        /// <param name="DrugNum"></param>
        /// <returns></returns>
        public Drug GetDrug(int DrugNum)
        {
            Drug obj = new Drug();
            obj.DrugTypeNum = new DrugType();
            obj.MeasuringUnitNum = new MeasuringUnit();
            IDataReader dr = drugDal.Select(DrugNum);
            while (dr != null && dr.Read())
            {
                obj.FetchObject(dr);
            }
            return obj;
        }

        /// <summary>
        /// 获取所有药品信息
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllDrug()
        {
            return drugDal.Select();
        }

        public DataTable GetAllDrug(string DrugType, string trade_name, string common_name, string drug_code)
        {
            return drugDal.Select(DrugType, trade_name, common_name, drug_code);
        }

        /// <summary>
        /// 根据各种条件查询药品
        /// </summary>
        /// <param name="DrugTypeName"></param>
        /// <param name="TradeName"></param>
        /// <param name="DrugCode"></param>
        /// <returns></returns>
        public DataTable Filter(string DrugTypeName, string TradeName, string DrugCode)
        {
            return drugDal.Select(DrugTypeName, TradeName, DrugCode);
        }

        /// <summary>
        /// 创建一种药品
        /// </summary>
        /// <param name="drug"></param>
        /// <returns></returns>
        //public bool CreateDrug(Drug drug)
        //{
        //    return drugDal.Insert(drug.DrugTypeNum.DrugTypeNum, drug.TradeName, drug.CommonName, drug.UserDefindeName, drug.DrugCode, drug.Sepcifications, drug.MeasuringUnitNum.MeasuringUnitNum, drug.ApprovalNum,drug.MinQuantity,drug.MaxQuantity,drug.TradePrice,drug.RetailPrice) > 0 ? true : false;
        //}

        /// <summary>
        /// 根据药品id删除药品
        /// </summary>
        /// <param name="drugNum"></param>
        /// <returns></returns>
        public bool RemoveDrug(int drugNum)
        {
            return drugDal.Delete(drugNum) > 0 ? true : false;
        }

        /// <summary>
        /// 更新drug
        /// </summary>
        /// <param name="drug"></param>
        /// <returns></returns>
        //public bool SetDrug(Drug drug)
        //{
        //    return drugDal.Update(drug.DrugNum, drug.DrugTypeNum.DrugTypeNum, drug.TradeName, drug.CommonName, drug.UserDefindeName, drug.DrugCode, drug.Sepcifications, drug.MeasuringUnitNum.MeasuringUnitNum, drug.ApprovalNum, drug.MinQuantity, drug.MaxQuantity,drug.TradePrice,drug.RetailPrice) > 0 ? true : false;
        //}


    }
}
